package com.getpebble.android.common.model;

import android.content.ContentValues;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable.Creator;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.provider.model.PblLockerAppDataModel;
import java.util.UUID;

public class LockerApp implements IWatchApp {
    public static final Creator<LockerApp> CREATOR = new Creator<LockerApp>() {
        public LockerApp createFromParcel(Parcel source) {
            return new LockerApp(source);
        }

        public LockerApp[] newArray(int size) {
            return new LockerApp[size];
        }
    };
    private static final String TAG = LockerApp.class.getSimpleName();
    private Companion android_companion;
    private String author;
    private String description;
    private String developer_id;
    private String href;
    private String icon_image;
    private String id;
    private Links links;
    private String list_image;
    private String locker_image;
    private String pbw_file;
    private String preview_image;
    private String release_id;
    private String screenshot_image;
    private String title;
    private String type;
    private String uuid;
    private String version;

    public static class Companion implements Parcelable {
        public static final Creator<Companion> CREATOR = new Creator<Companion>() {
            public Companion createFromParcel(Parcel source) {
                return new Companion(source);
            }

            public Companion[] newArray(int size) {
                return new Companion[size];
            }
        };
        public String icon;
        public String id;
        public String name;
        public String url;

        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("{ id:").append(this.id);
            builder.append(", name:").append(this.name);
            builder.append(", icon:").append(this.icon);
            builder.append(", url:").append(this.url);
            builder.append(" }");
            return builder.toString();
        }

        public int describeContents() {
            return 0;
        }

        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.id);
            dest.writeString(this.name);
            dest.writeString(this.icon);
            dest.writeString(this.url);
        }

        private Companion(Parcel in) {
            this.id = in.readString();
            this.name = in.readString();
            this.icon = in.readString();
            this.url = in.readString();
        }
    }

    public static class Links implements Parcelable {
        public static final Creator<Links> CREATOR = new Creator<Links>() {
            public Links createFromParcel(Parcel source) {
                return new Links(source);
            }

            public Links[] newArray(int size) {
                return new Links[size];
            }
        };
        public String add;
        public String remove;

        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("{ add:").append(this.add);
            builder.append(", remove:").append(this.remove);
            builder.append(" }");
            return builder.toString();
        }

        public int describeContents() {
            return 0;
        }

        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(this.add);
            dest.writeString(this.remove);
        }

        private Links(Parcel in) {
            this.add = in.readString();
            this.remove = in.readString();
        }
    }

    public String getId() {
        return this.id;
    }

    public String getAppName() {
        return this.title;
    }

    public String getScreenshotImage() {
        return this.screenshot_image;
    }

    public String getCompanyName() {
        return this.author;
    }

    public String getDeveloperId() {
        return this.developer_id;
    }

    public String getDescription() {
        return this.description;
    }

    public String getPreviewImage() {
        return this.preview_image;
    }

    public String getIconImage() {
        return this.icon_image;
    }

    public UUID getUuid() {
        return this.uuid != null ? UUID.fromString(this.uuid) : null;
    }

    public boolean isLoaded() {
        return false;
    }

    public String getPbwFile() {
        return this.pbw_file;
    }

    public String getVersion() {
        return this.version;
    }

    public boolean hasCompanionApp() {
        return this.android_companion != null;
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("{ id:").append(this.id);
        builder.append(", release_id:").append(this.release_id);
        builder.append(", title:").append(this.title);
        builder.append(", uuid:").append(this.uuid);
        builder.append(", icon_image:").append(this.icon_image);
        builder.append(", list_image:").append(this.list_image);
        builder.append(", type:").append(this.type);
        builder.append(", pbw_file:").append(this.pbw_file);
        builder.append(", href:").append(this.href);
        builder.append(", author:").append(this.author);
        builder.append(", description:").append(this.description);
        builder.append(", locker_image:").append(this.locker_image);
        builder.append(", preview_image:").append(this.preview_image);
        builder.append(", screenshot_image:").append(this.screenshot_image);
        builder.append(", android_companion:").append(this.android_companion);
        builder.append(", developer_id:").append(this.developer_id);
        builder.append(", version:").append(this.version);
        builder.append(", links:").append(this.links);
        builder.append(" }");
        return builder.toString();
    }

    public ContentValues toContentValues() {
        ContentValues contentValues = new ContentValues(new PblLockerAppDataModel().getNumColumns());
        contentValues.put("app_id", this.id);
        contentValues.put("release_id", this.release_id);
        contentValues.put("title", this.title);
        contentValues.put("uuid", this.uuid);
        contentValues.put("icon_image", this.icon_image);
        contentValues.put("list_image", this.list_image);
        contentValues.put("type", this.type);
        contentValues.put("pbw_file", this.pbw_file);
        contentValues.put("href", this.href);
        contentValues.put("author", this.author);
        contentValues.put("developer_id", this.developer_id);
        contentValues.put("description", this.description);
        contentValues.put("locker_image", this.locker_image);
        contentValues.put("preview_image", this.preview_image);
        contentValues.put("screenshot_image", this.screenshot_image);
        contentValues.put("version", this.version);
        if (this.android_companion != null) {
            contentValues.put("companion_id", this.android_companion.id);
            contentValues.put("companion_name", this.android_companion.name);
            contentValues.put("companion_icon", this.android_companion.icon);
            contentValues.put("companion_url", this.android_companion.url);
        }
        if (this.links != null) {
            contentValues.put("add_link", this.links.add);
            contentValues.put("remove_link", this.links.remove);
        }
        return contentValues;
    }

    public static LockerApp fromContentValues(ContentValues contentValues) {
        if (contentValues == null) {
            Trace.error(TAG, "Attempting to build WatchApp from null contentValues");
            return null;
        }
        LockerApp lockerApp = new LockerApp();
        lockerApp.id = contentValues.getAsString("app_id");
        lockerApp.release_id = contentValues.getAsString("release_id");
        lockerApp.title = contentValues.getAsString("title");
        lockerApp.uuid = contentValues.getAsString("uuid");
        lockerApp.icon_image = contentValues.getAsString("icon_image");
        lockerApp.list_image = contentValues.getAsString("list_image");
        lockerApp.type = contentValues.getAsString("type");
        lockerApp.pbw_file = contentValues.getAsString("pbw_file");
        lockerApp.href = contentValues.getAsString("href");
        lockerApp.author = contentValues.getAsString("author");
        lockerApp.developer_id = contentValues.getAsString("developer_id");
        lockerApp.description = contentValues.getAsString("description");
        lockerApp.locker_image = contentValues.getAsString("locker_image");
        lockerApp.preview_image = contentValues.getAsString("preview_image");
        lockerApp.screenshot_image = contentValues.getAsString("screenshot_image");
        lockerApp.version = contentValues.getAsString("version");
        String companionName = contentValues.getAsString("companion_name");
        if (companionName != null) {
            Companion companion = new Companion();
            companion.id = contentValues.getAsString("companion_id");
            companion.name = companionName;
            companion.icon = contentValues.getAsString("companion_icon");
            companion.url = contentValues.getAsString("companion_url");
            lockerApp.android_companion = companion;
        }
        String addLink = contentValues.getAsString("add_link");
        String removeLink = contentValues.getAsString("remove_link");
        if (addLink == null && removeLink == null) {
            return lockerApp;
        }
        Links links = new Links();
        links.add = addLink;
        links.remove = removeLink;
        lockerApp.links = links;
        return lockerApp;
    }

    public boolean isValid() {
        if (this.id == null || this.uuid == null) {
            return false;
        }
        return true;
    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        dest.writeString(this.release_id);
        dest.writeString(this.title);
        dest.writeString(this.uuid);
        dest.writeString(this.icon_image);
        dest.writeString(this.list_image);
        dest.writeString(this.type);
        dest.writeString(this.pbw_file);
        dest.writeString(this.href);
        dest.writeString(this.author);
        dest.writeString(this.description);
        dest.writeString(this.locker_image);
        dest.writeString(this.preview_image);
        dest.writeString(this.screenshot_image);
        dest.writeString(this.developer_id);
        dest.writeString(this.version);
        dest.writeParcelable(this.android_companion, 0);
        dest.writeParcelable(this.links, 0);
    }

    private LockerApp(Parcel in) {
        this.id = in.readString();
        this.release_id = in.readString();
        this.title = in.readString();
        this.uuid = in.readString();
        this.icon_image = in.readString();
        this.list_image = in.readString();
        this.type = in.readString();
        this.pbw_file = in.readString();
        this.href = in.readString();
        this.author = in.readString();
        this.description = in.readString();
        this.locker_image = in.readString();
        this.preview_image = in.readString();
        this.screenshot_image = in.readString();
        this.developer_id = in.readString();
        this.version = in.readString();
        this.android_companion = (Companion) in.readParcelable(Companion.class.getClassLoader());
        this.links = (Links) in.readParcelable(Links.class.getClassLoader());
    }
}
